﻿using System;
using System.Collections.Generic;
using FireFly;
using System.Reflection;
using System.Collections;
using System.Windows;
using System.Linq;

namespace XAMLStylePad.ViewModels
{
    public class ConstructorViewModel : ViewModelBase<ConstructorInfo>
    {
        public ConstructorViewModel(ConstructorInfo ctor)
            : base(ctor)
        {
        }

        #region Parameters
        public CollectionViewModel<ParameterInfo, ParameterViewModel> Parameters
        {
            get
            {
                if(_Parameters == null)
                {
                    _Parameters = new CollectionViewModel<ParameterInfo, ParameterViewModel>(Model.GetParameters(), "Parameters", CollectionShowMode.UserFriendly, model =>
                    {
                        var viewModel = new ParameterViewModel(model);
                        return viewModel;
                    });
                }
                return _Parameters;
            }
        }
        CollectionViewModel<ParameterInfo, ParameterViewModel> _Parameters;
        #endregion

        public override string DisplayName
        {
            get 
            { 
                var list = from p in Model.GetParameters()
                           select string.Format("{0} {1}", p.ParameterType.Name, p.Name);

                string res = string.Join(", ", list.ToArray());
                return string.IsNullOrEmpty(res) ? "<parameterless constructor>": res;
                //string res = string.Format()
            }
        }

        internal object CreateObject(params object[] args)
        {
            return Model.Invoke(args);
        }
    }
}